home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / jpsrc2.zip / JDMASTER.C < prev    next >
C/C++ Source or Header  |  1991-10-03  |  6KB  |  181 lines

  1. /*
  2.  * jdmaster.c
  3.  *
  4.  * Copyright (C) 1991, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains the main control for the JPEG decompressor.
  9.  * The system-dependent (user interface) code should call jpeg_decompress()
  10.  * after doing appropriate setup of the decompress_info_struct parameter.
  11.  */
  12.  
  13. #include "jinclude.h"
  14.  
  15.  
  16. METHODDEF void
  17. d_per_scan_method_selection (decompress_info_ptr cinfo)
  18. /* Central point for per-scan method selection */
  19. {
  20.   /* MCU disassembly */
  21.   jseldmcu(cinfo);
  22.   /* Un-subsampling of pixels */
  23.   jselunsubsample(cinfo);
  24. }
  25.  
  26.  
  27. LOCAL void
  28. d_initial_method_selection (decompress_info_ptr cinfo)
  29. /* Central point for initial method selection (after reading file header) */
  30. {
  31.   /* JPEG file scanning method selection is already done. */
  32.   /* So is output file format selection (both are done by user interface). */
  33.  
  34.   /* Entropy decoding: either Huffman or arithmetic coding. */
  35. #ifdef ARITH_CODING_SUPPORTED
  36.   jseldarithmetic(cinfo);
  37. #else
  38.   if (cinfo->arith_code) {
  39.     ERREXIT(cinfo->emethods, "Arithmetic coding not supported");
  40.   }
  41. #endif
  42.   jseldhuffman(cinfo);
  43.   /* Cross-block smoothing */
  44. #ifdef BLOCK_SMOOTHING_SUPPORTED
  45.   jselbsmooth(cinfo);
  46. #else
  47.   cinfo->do_block_smoothing = FALSE;
  48. #endif
  49.   /* Gamma and color space conversion */
  50.   jseldcolor(cinfo);
  51.  
  52.   /* Color quantization */
  53. #ifdef QUANT_1PASS_SUPPORTED
  54. #ifndef QUANT_2PASS_SUPPORTED
  55.   cinfo->two_pass_quantize = FALSE; /* only have 1-pass */
  56. #endif
  57. #else /* not QUANT_1PASS_SUPPORTED */
  58. #ifdef QUANT_2PASS_SUPPORTED
  59.   cinfo->two_pass_quantize = TRUE; /* only have 2-pass */
  60. #else /* not QUANT_2PASS_SUPPORTED */
  61.   if (cinfo->quantize_colors) {
  62.     ERREXIT(cinfo->emethods, "Color quantization was not compiled");
  63.   }
  64. #endif
  65. #endif
  66.  
  67. #ifdef QUANT_1PASS_SUPPORTED
  68.   jsel1quantize(cinfo);
  69. #endif
  70. #ifdef QUANT_2PASS_SUPPORTED
  71.   jsel2quantize(cinfo);
  72. #endif
  73.  
  74.   /* Pipeline control */
  75.   jseldpipeline(cinfo);
  76.   /* Overall control (that's me!) */
  77.   cinfo->methods->d_per_scan_method_selection = d_per_scan_method_selection;
  78. }
  79.  
  80.  
  81. LOCAL void
  82. initial_setup (decompress_info_ptr cinfo)
  83. /* Do computations that are needed before initial method selection */
  84. {
  85.   short ci;
  86.   jpeg_component_info *compptr;
  87.  
  88.   /* Compute maximum sampling factors; check factor validity */
  89.   cinfo->max_h_samp_factor = 1;
  90.   cinfo->max_v_samp_factor = 1;
  91.   for (ci = 0; ci < cinfo->num_components; ci++) {
  92.     compptr = &cinfo->comp_info[ci];
  93.     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
  94.     compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
  95.       ERREXIT(cinfo->emethods, "Bogus sampling factors");
  96.     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
  97.                    compptr->h_samp_factor);
  98.     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
  99.                    compptr->v_samp_factor);
  100.  
  101.   }
  102.  
  103.   /* Compute logical subsampled dimensions of components */
  104.   for (ci = 0; ci < cinfo->num_components; ci++) {
  105.     compptr = &cinfo->comp_info[ci];
  106.     compptr->true_comp_width = (cinfo->image_width * compptr->h_samp_factor
  107.                 + cinfo->max_h_samp_factor - 1)
  108.                 / cinfo->max_h_samp_factor;
  109.     compptr->true_comp_height = (cinfo->image_height * compptr->v_samp_factor
  110.                  + cinfo->max_v_samp_factor - 1)
  111.                  / cinfo->max_v_samp_factor;
  112.   }
  113. }
  114.  
  115.  
  116. /*
  117.  * This is the main entry point to the JPEG decompressor.
  118.  */
  119.  
  120.  
  121. GLOBAL void
  122. jpeg_decompress (decompress_info_ptr cinfo)
  123. {
  124.   short i;
  125.  
  126.   /* Initialize pointers as needed to mark stuff unallocated. */
  127.   cinfo->comp_info = NULL;
  128.   for (i = 0; i < NUM_QUANT_TBLS; i++)
  129.     cinfo->quant_tbl_ptrs[i] = NULL;
  130.   for (i = 0; i < NUM_HUFF_TBLS; i++) {
  131.     cinfo->dc_huff_tbl_ptrs[i] = NULL;
  132.     cinfo->ac_huff_tbl_ptrs[i] = NULL;
  133.   }
  134.  
  135.   /* Read the JPEG file header markers; everything up through the first SOS
  136.    * marker is read now.  NOTE: the user interface must have initialized the
  137.    * read_file_header method pointer (eg, by calling jselrjfif or jselrtiff).
  138.    * The other file reading methods (read_scan_header etc.) were probably
  139.    * set at the same time, but could be set up by read_file_header itself.
  140.    */
  141.   (*cinfo->methods->read_file_header) (cinfo);
  142.   if (! ((*cinfo->methods->read_scan_header) (cinfo)))
  143.     ERREXIT(cinfo->emethods, "Empty JPEG file");
  144.  
  145.   /* Give UI a chance to adjust decompression parameters and select */
  146.   /* output file format based on info from file header. */
  147.   (*cinfo->methods->d_ui_method_selection) (cinfo);
  148.  
  149.   /* Now select methods for decompression steps. */
  150.   initial_setup(cinfo);
  151.   d_initial_method_selection(cinfo);
  152.  
  153.   /* Initialize the output file & other modules as needed */
  154.   /* (color_quant and entropy_decoder are inited by pipeline controller) */
  155.  
  156.   (*cinfo->methods->output_init) (cinfo);
  157.   (*cinfo->methods->colorout_init) (cinfo);
  158.  
  159.   /* And let the pipeline controller do the rest. */
  160.   (*cinfo->methods->d_pipeline_controller) (cinfo);
  161.  
  162.   /* Finish output file, release working storage, etc */
  163.   (*cinfo->methods->colorout_term) (cinfo);
  164.   (*cinfo->methods->output_term) (cinfo);
  165.   (*cinfo->methods->read_file_trailer) (cinfo);
  166.  
  167.   /* Release allocated storage for tables */
  168. #define FREE(ptr)  if ((ptr) != NULL) \
  169.             (*cinfo->emethods->free_small) ((void *) ptr)
  170.  
  171.   FREE(cinfo->comp_info);
  172.   for (i = 0; i < NUM_QUANT_TBLS; i++)
  173.     FREE(cinfo->quant_tbl_ptrs[i]);
  174.   for (i = 0; i < NUM_HUFF_TBLS; i++) {
  175.     FREE(cinfo->dc_huff_tbl_ptrs[i]);
  176.     FREE(cinfo->ac_huff_tbl_ptrs[i]);
  177.   }
  178.  
  179.   /* My, that was easy, wasn't it? */
  180. }
  181.